/**
 * Created by IntelliJ IDEA.
 * User: fujunjie
 * Date: Aug 27, 2003
 * Time: 3:54:41 PM
 * To change this template use Options | File Templates.
 */
package com.ecomm.util;

import org.apache.commons.logging.Log;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.util.HashMap;
import java.util.List;

public class MimeType
{
    private static Log log = Logger.getLog(MimeType.class);
    private static HashMap hash = null;

    static
    {
        System.out.println("\n开始装载MimeType文件..........");
        String fileName = "mimetype.xml";
        try
        {
            hash = new HashMap();

            String filePath = System.getProperty("ECOMM_HOME");
            if (filePath == null)
            {
                System.out.println("没有指定环境变量ECOMM_HOME。");
            }
            else
            {
                SAXBuilder builder = new SAXBuilder();
                Document doc = builder.build(new File(filePath + "/" + fileName));
                Element root = doc.getRootElement();
                List ls = root.getChildren();
                Element element = null;
                for (int i = 0; i < ls.size(); i++)
                {
                    element = (Element) ls.get(i);
                    hash.put(element.getChild("extension").getText(), element.getChild("mime-type").getText());
                }
            }
            System.out.println("装载MimeType文件结束！");
        }
        catch (Exception e)
        {
            log.error("解析xml文件出错，文件名为：" + fileName + "   !", e);
        }
    }

    public static String getMimeType(String fileExtension)
    {
        if (hash.get(fileExtension) == null)
        {
            return null;
        }
        return hash.get(fileExtension).toString();
    }

    public static void main(String[] args)
    {
        System.out.println(MimeType.getMimeType("aiff"));

    }
}
